#include "buttonlamp.h"
#include <QPainter>

ButtonLamp::ButtonLamp(int index, QGraphicsItem *parent, int lampradius)
: QGraphicsWidget(parent), mIndex(index), mStatus(RELAY_STATUS_UNKNOWN)
{
    mLampRadius = lampradius;
    setCacheMode(DeviceCoordinateCache);
}

void ButtonLamp::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
    QRectF r = boundingRect();
    QRadialGradient radialGradient(0, 0, mLampRadius, 0, 0);
    if(RELAY_STATUS_OFF == mStatus){
        radialGradient.setColorAt(0.0, Qt::white);
        radialGradient.setColorAt(0.2, Qt::yellow);
        radialGradient.setColorAt(0.8, Qt::darkYellow);
        painter->setPen(Qt::darkYellow);
        painter->setOpacity(0.5);
    } else if (RELAY_STATUS_ON == mStatus){
        radialGradient.setColorAt(0.0, Qt::white);
        radialGradient.setColorAt(0.5, Qt::yellow);
        radialGradient.setColorAt(1.0, Qt::darkYellow);
        painter->setPen(Qt::darkYellow);
        painter->setOpacity(0.8);
    } else {
        radialGradient.setColorAt(0.0, Qt::darkGray);
        radialGradient.setColorAt(0.2, Qt::lightGray);
        radialGradient.setColorAt(0.8, Qt::white);
        painter->setPen(Qt::white);
        painter->setOpacity(0.8);
    }
    painter->setBrush(radialGradient);
    painter->drawEllipse(r);
}

int ButtonLamp::getIndex()
{
    return mIndex;
}

void ButtonLamp::setStatus(int status)
{
    mStatus = status;
}
